//  -----------------------------------------------------------------------------------------------//
//   This file is part of SvnExternalUpdater                                                               //
//   Copyright (C)2008 Romain Verdier ( romain@codingly.com )                                      //
//                                                                                                 //
//   This program is free software; you can redistribute it and/or modify it under the terms       //
//   of the GNU General Public License as published by the Free Software Foundation; either        //
//   version 2 of the License, or (at your option) any later version. This program is distributed  //
//   in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied        //
//   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public  //
//   License for more details.                                                                     //
//                                                                                                 //
//   You should have received a copy of the GNU General Public License along with this program; if //
//   not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. or  //
//   see http://www.fsf.org/licensing/licenses/gpl.html                                            //
//  -----------------------------------------------------------------------------------------------//
using System.Collections.Generic;

namespace Codingly.SvnExternalUpdater.Core
{
	internal class Cache<T> where T : class, IRemoteConnected
	{
		private readonly Dictionary<IRemoteFolder, List<T>> cacheByFolder = new Dictionary<IRemoteFolder, List<T>>();
		private readonly Dictionary<string, T> cacheById = new Dictionary<string, T>();

		public IList<T> All
		{
			get { return new List<T>(this.cacheById.Values); }
		}

		public bool AllIsUpToDate
		{
			get
			{
				var items = new List<T>(this.cacheById.Values);
				return items.TrueForAll(i => i.IsUpToDate);
			}
		}

		public IList<string> Ids
		{
			get { return new List<string>(this.cacheById.Keys); }
		}

		public void Add(T item)
		{
			List<T> folderList = GetOrCreateFolderList(item.RemoteFolder);
			folderList.Add(item);
			this.cacheById.Add(item.Id, item);
		}

		public bool Contains(string id)
		{
			return this.cacheById.ContainsKey(id);
		}

		public bool Contains(IRemoteFolder remoteFolder)
		{
			return this.cacheByFolder.ContainsKey(remoteFolder);
		}

		public T GetByPath(string id)
		{
			T item;
			if (this.cacheById.TryGetValue(id, out item))
			{
				return item;
			}
			return null;
		}

		public IEnumerable<T> GetByRemoteFolder(IRemoteFolder remoteFolder)
		{
			List<T> items;
			if (!this.cacheByFolder.TryGetValue(remoteFolder, out items))
			{
				items = new List<T>();
			}
			return items;
		}

		public void Remove(string path)
		{
			T item;
			if (this.cacheById.TryGetValue(path, out item))
			{
				this.cacheById.Remove(path);
				RemoveItemFromCacheByFolder(item);
			}
		}

		private List<T> GetOrCreateFolderList(IRemoteFolder remoteFolder)
		{
			List<T> folderList;
			if (!this.cacheByFolder.TryGetValue(remoteFolder, out folderList))
			{
				folderList = new List<T>();
				this.cacheByFolder.Add(remoteFolder, folderList);
			}
			return folderList;
		}

		private void RemoveItemFromCacheByFolder(T item)
		{
			List<T> items;
			if (this.cacheByFolder.TryGetValue(item.RemoteFolder, out items))
			{
				items.Remove(item);
				if (items.Count == 0)
				{
					this.cacheByFolder.Remove(item.RemoteFolder);
				}
			}
		}
	}
}